//package com.mzq.langchain4j_springboot.web.controller;
//
//
//import com.mzq.langchain4j_springboot.config.AiConfig;
//import dev.langchain4j.data.message.AiMessage;
//import dev.langchain4j.data.message.ImageContent;
//import dev.langchain4j.data.message.TextContent;
//import dev.langchain4j.data.message.UserMessage;
//import dev.langchain4j.model.chat.ChatModel;
//
//import dev.langchain4j.model.chat.ChatModel;
//import dev.langchain4j.model.chat.StreamingChatModel;
//import dev.langchain4j.model.chat.StreamingChatModel;
//import dev.langchain4j.model.chat.response.ChatResponse;
//import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
//import dev.langchain4j.service.TokenStream;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import reactor.core.publisher.Flux;
//
//import java.time.LocalDate;
//
//@RequestMapping("/ollama")
//@RestController
//public class ChatController {
//
//    @Autowired
//    ChatModel chatLanguageModel;
//
//    @Autowired(required=true)
//    StreamingChatModel qwenStreamingChatModel;
//
//    @Autowired
//    AiConfig.Assistant assistant;
//
//    @Autowired
//    AiConfig.EquityAssistant equityAssistant;
//
//
//    //1.尝试模型能否记住对话 上一次对话告诉模型我名字了 但是无法记住
//    @GetMapping("/test")
//    public String test() {
//        chatLanguageModel.chat("你好，我是马振清");
//        return chatLanguageModel.chat("你好，我是谁");
//    }
//
//    /**
//     * curl -i localhost:8080/ollama/chat?prompt=今天日期是多少
//     * @param prompt
//     * @return
//     */
//    //2.尝试调用ollama集成的模型
//    @GetMapping("/chat")
//    public String chat(@RequestParam("prompt") String prompt) {
//        return chatLanguageModel.chat(prompt);
//    }
//
//    //3.模仿模型的流式对话 一个一个字往外蹦
////    @RequestMapping(value ="/streamChat",produces ="text/stream;charset=UTF-8")
//    @GetMapping(value ="/stream",produces ="text/stream;charset=UTF-8")
//    public Flux<String> streamOutput(@RequestParam(name = "message",defaultValue="你是谁")String message) {
//        //流式响应处理器
//        return Flux.create(sink->{
//            qwenStreamingChatModel.chat(message, new StreamingChatResponseHandler() {
//                @Override
//                public void onPartialResponse(String s) {
//                    sink.next(s);//逐次返回部分响应
//                }
//
//                @Override
//                public void onCompleteResponse(ChatResponse chatResponse) {
//                    sink.complete();//完成整个响应流
//                }
//
//                @Override
//                public void onError(Throwable throwable) {
//                    sink.error(throwable);//异常处理
//                }
//            });
//        });
//    }
//
//    //4.多轮对话，并记住之前内容
//    @GetMapping("/chats")
//    public String chats() {
//        UserMessage userMessage1=UserMessage.userMessage("你好，我是马振清");
//        ChatResponse response1=chatLanguageModel.chat(userMessage1);
//        AiMessage aiMessage1=response1.aiMessage();//大模型的第一次响应
//        System.out.println(aiMessage1.text());
//        System.out.println("------");
//        //下面一行代码是重点
//        ChatResponse response2=chatLanguageModel.chat(userMessage1,aiMessage1,UserMessage.userMessage("我叫什么"));
//        AiMessage aiMessage2=response2.aiMessage();//大模型的第二次响应
//        System.out.println(aiMessage2.text());
//        return aiMessage2.text();
//    }
//
//    //5.尝试用ChatMemory 这个不是流式
//    @RequestMapping(value = "/memory_chat")
//    public String memoryChat(@RequestParam(name = "message",defaultValue="我叫马振清") String message) {
//        UserMessage userMessage = UserMessage.from(
//                TextContent.from("图片存在什么安全隐患"),
//                ImageContent.from("https://tjnykjgs.oss-cn-beijing.aliyuncs.com/15087bcf-755a-47d9-affc-7fef6dc5bfdc.png")
//        );
//        return assistant.chat(userMessage);
//    }
//
//    //6.尝试用ChatMemory 这个是流式
//    @RequestMapping(value = "/memory_stream_chat",produces ="text/stream;charset=UTF-8")
//    public Flux<String> memoryStreamChat(@RequestParam(name = "message",defaultValue="我是谁") String message, HttpServletResponse response) {
//        TokenStream stream = assistant.stream(message);
//
//        return Flux.create(sink -> {
//            //新语法记录一下
////            stream.onPartialResponse(s -> sink.next(s))
//            stream.onPartialResponse(sink::next)
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        });
//    }
//
//    //1-6其实都存储在内存中，断电就会丢失
//
//    //7.票务系统小助手
//    @RequestMapping(value = "/memory_stream_chat_pw",produces ="text/stream;charset=UTF-8")
//    public Flux<String> memoryStreamChatByPW(@RequestParam(name="message",defaultValue="我是谁") String message, HttpServletResponse response) {
//        TokenStream stream = assistant.stream(message, LocalDate.now().toString());
//        return Flux.create(sink -> {
//            stream.onPartialResponse(s -> sink.next(s))
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        });
//    }
//
//    @RequestMapping(value = "/memory_stream_chat_DB",produces ="text/stream;charset=UTF-8")
//    public Flux<String> memoryStreamChatByDB(@RequestParam(name = "message",defaultValue="我是谁") String message, HttpServletResponse response) {
//        TokenStream stream = assistant.stream(message);
//        return Flux.create(sink -> {
//            stream.onPartialResponse(s -> sink.next(s))
//                    .onCompleteResponse(c -> sink.complete())
//                    .onError(sink::error)
//                    .start();
//
//        });
//    }
//}
